package com.winhxd.b2c.admin.module.system.controller;

import com.winhxd.b2c.admin.common.context.UserManager;
import com.winhxd.b2c.admin.common.security.annotation.CheckPermission;
import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.domain.PagedList;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.system.org.condition.SysOrgCondition;
import com.winhxd.b2c.common.domain.system.security.enums.PermissionEnum;
import com.winhxd.b2c.common.domain.system.user.model.SysOrg;
import com.winhxd.b2c.common.domain.system.user.vo.UserInfo;
import com.winhxd.b2c.common.feign.system.OrgServiceClient;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 部门
 * @author shumin
 * @create 2019/1/7 0007 19:24
 */
@RestController
public class OrgController {
    private static final Logger logger = LoggerFactory.getLogger(OrgController.class);

    private static final String MODULE_NAME = "部门管理管理";

    @Autowired
    private OrgServiceClient orgServiceClient;
    /* *
     *部门列表
     * @author shumin
     * @date  2019/1/7 0007 19:26
    */
    @ApiResponses({
            @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1002, message = "登录凭证无效"),
            @ApiResponse(code = BusinessCode.CODE_1003, message = "没有权限")
    })
    @PostMapping(value = "/org/list")
    @CheckPermission({PermissionEnum.SYSTEM_STAFF_ORG})
    public ResponseResult<PagedList<SysOrg>> list(@RequestBody SysOrgCondition condition){
        logger.info("{} - 部门列表- 条件:condition={}", MODULE_NAME, condition.toString());
        return orgServiceClient.findOrgPageList(condition);
    }

    /* *
     *根据id获取部门
     * @author shumin
     * @date  2019/1/7 0007 20:08
    */
    @ApiResponses({
            @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1002, message = "登录凭证无效"),
            @ApiResponse(code = BusinessCode.CODE_1003, message = "没有权限")
    })
    @GetMapping(value = "/org/get/{id}")
    @CheckPermission({PermissionEnum.SYSTEM_STAFF_ORG})
    public ResponseResult<SysOrg> get(@PathVariable("id") Long id){
        logger.info("{} - 根据id查询部门件:id={}", MODULE_NAME, id);
        return orgServiceClient.getById(id);
    }
    /* *
     *新增部门
     * @author shumin
     * @date  2019/1/7 0007 19:47
    */
    @ApiResponses({
            @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1002, message = "登录凭证无效"),
            @ApiResponse(code = BusinessCode.CODE_1003, message = "没有权限")
    })
    @PostMapping(value = "/org/save")
    @CheckPermission({PermissionEnum.SYSTEM_STAFF_ORG_ADD})
    public ResponseResult<Void> save(@RequestBody SysOrg sysOrg){
        logger.info("{} - 新增部门- 条件:SysOrg={}", MODULE_NAME, sysOrg.toString());
        UserInfo userInfo = UserManager.getCurrentUser();
        sysOrg.setCreatedBy(userInfo.getId());
        sysOrg.setCreatedByName(userInfo.getUsername());
        sysOrg.setCreated(new Date());
        return orgServiceClient.saveOrg(sysOrg);
    }

    /* *
     *修改部门
     * @author shumin
     * @date  2019/1/7 0007 19:49
    */
    @ApiResponses({
            @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1002, message = "登录凭证无效"),
            @ApiResponse(code = BusinessCode.CODE_1003, message = "没有权限")
    })
    @PostMapping(value = "/org/edit")
    @CheckPermission({PermissionEnum.SYSTEM_STAFF_ORG_EDIT})
    public ResponseResult<Void> edit(@RequestBody SysOrg sysOrg){
        logger.info("{} - 修改部门- 条件:SysOrg={}", MODULE_NAME, sysOrg.toString());
        UserInfo userInfo = UserManager.getCurrentUser();
        sysOrg.setUpdatedByName(userInfo.getUsername());
        sysOrg.setUpdatedBy(userInfo.getId());
        sysOrg.setUpdated(new Date());
        return orgServiceClient.editOrg(sysOrg);
    }

    /* *
     *删除部门
     * @author shumin
     * @date  2019/1/7 0007 19:49
    */
    @ApiResponses({
            @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1002, message = "登录凭证无效"),
            @ApiResponse(code = BusinessCode.CODE_1003, message = "没有权限")
    })
    @DeleteMapping(value = "/org/delete/{id}")
    @CheckPermission({PermissionEnum.SYSTEM_STAFF_ORG_DELETE})
    public ResponseResult<Void> delete(@PathVariable("id") Long id){
        logger.info("{} - 删除部门- 条件:id={}", MODULE_NAME, id);
        SysOrg sysOrg = new SysOrg();
        sysOrg.setId(id);
        UserInfo userInfo = UserManager.getCurrentUser();
        sysOrg.setUpdatedByName(userInfo.getUsername());
        sysOrg.setUpdatedBy(userInfo.getId());
        sysOrg.setUpdated(new Date());
        return orgServiceClient.deleteOrg(sysOrg);
    }

    @ApiResponses({
            @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1002, message = "登录凭证无效"),
            @ApiResponse(code = BusinessCode.CODE_1003, message = "没有权限")
    })
    @PostMapping(value = "/org/allList")
    @CheckPermission({PermissionEnum.SYSTEM_STAFF_ORG})
    public ResponseResult<PagedList<SysOrg>> allList(@RequestBody SysOrgCondition condition){
        logger.info("{} - 部门列表- 条件:condition={}", MODULE_NAME, condition.toString());
        condition.setId(null);
        condition.setQueryType(null);
        return orgServiceClient.findOrgPageList(condition);
    }
    @ApiResponses({
            @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1002, message = "登录凭证无效"),
            @ApiResponse(code = BusinessCode.CODE_1003, message = "没有权限")
    })
    @PostMapping(value = "/org/findOrgList")
    @CheckPermission({PermissionEnum.SYSTEM_STAFF_ORG})
    public ResponseResult<List<SysOrg>> findOrgList(@RequestBody SysOrgCondition condition){
        logger.info("{} - 部门列表,所有部门- 条件:condition={}", MODULE_NAME, condition.toString());
        condition.setQueryType(null);
        return orgServiceClient.findOrgList(condition);
    }


}
